<template>
  <div class="confirmOrder">
    <a-card title="订单信息">
      <a-form layout="inline" :model="formObj">
        <a-form-item>
          <a-date-picker
            v-model:value="formObj.date"
            value-format="YYYY-MM-DD"
            placeholder="请选择日期"
          />
        </a-form-item>
        <a-form-item>
          <TrainSelect v-model:code="formObj.trainCode" width="200px" />
        </a-form-item>
        <a-form-item>
          <StationSelect v-model:name="formObj.start" width="200px" placeholder="请选择出发站" />
        </a-form-item>
        <a-form-item>
          <StationSelect v-model:name="formObj.end" width="200px" placeholder="请选择到达站" />
        </a-form-item>
        <a-form-item>
          <a-button type="primary" @click="getList">查询</a-button>
        </a-form-item>
        <a-form-item>
          <a-button @click="reset">重置</a-button>
        </a-form-item>
      </a-form>
    </a-card>
    <a-table :data-source="dataList" :columns="columns" :pagination="false">
      <template #bodyCell="{ column, record }">
        <template v-if="column.dataIndex === 'operation'"></template>
        <template v-else-if="column.dataIndex === 'status'">
          {{ getStatus(record.status) }}
        </template>
        <!-- <template v-else-if="column.dataIndex === 'tickets'">
          {{ record.tickets }}
        </template> -->
        <template v-else-if="column.dataIndex === 'create_time'">
          {{ record.createTime }}
        </template>
        <template v-else-if="column.dataIndex === 'update_time'">
          {{ record.updateTime }}
        </template>
      </template>
    </a-table>
    <a-pagination
      v-model:current="formObj.page"
      v-model:page-size="formObj.pageSize"
      align="right"
      :total="formObj.total"
      show-size-changer
      style="margin-top: 20px"
      @change="onChange"
    />
  </div>
</template>
<script setup lang="ts">
import { onMounted, ref } from 'vue';
import { ConfirmOrderApi } from '../../api/confirmOrder';
import TrainSelect from '@/components/Train-Select.vue';
import StationSelect from '../../components/Station-Select.vue';
import { getEnumList } from '@/utils/getEnum';

const columns = ref([
  {
    title: '会员id',
    dataIndex: 'memberId',
    key: 'memberId',
    width: 100,
  },
  {
    title: '日期',
    dataIndex: 'date',
    key: 'date',
    width: 100,
  },
  {
    title: '车次编号',
    dataIndex: 'trainCode',
    key: 'trainCode',
    width: 120,
  },
  {
    title: '出发站',
    dataIndex: 'start',
    key: 'start',
    width: 120,
  },
  {
    title: '到达站',
    dataIndex: 'end',
    key: 'end',
    width: 120,
  },
  {
    title: '余票ID',
    dataIndex: 'dailyTrainTicketId',
    key: 'dailyTrainTicketId',
    width: 120,
  },
  // {
  //   title: '车票',
  //   dataIndex: 'tickets',
  //   key: 'tickets',
  //   width: 150,
  // },
  {
    title: '订单状态',
    dataIndex: 'status',
    key: 'status',
    width: 100,
    customRender: (text: string) => {
      return getEnumList('confirm_order_status').value.find((item: any) => item.code == text)
        ?.description;
    },
  },
]);

const getStatus = (status: string): string => {
  return getEnumList('confirm_order_status').value.find((item: any) => item.code == status)
    ?.description;
};

const formObj = ref({
  date: null,
  trainCode: '',
  start: '',
  end: '',
  page: 1,
  pageSize: 10,
  total: 0,
});

onMounted(() => {
  getList();
});

const dataList = ref([]);

const getList = async (): Promise<void> => {
  const { data } = await ConfirmOrderApi.query(formObj.value);
  dataList.value = data.records || [];
  formObj.value.total = data.total;
};

const reset = (): void => {
  formObj.value.trainCode = '';
  formObj.value.start = '';
  formObj.value.end = '';
  formObj.value.date = null;
  getList();
};

const onChange = (current: number, pageSize: number): void => {
  console.log(current, pageSize);
  formObj.value.page = current;
  formObj.value.pageSize = pageSize;
  getList();
};
</script>
